package com.ews.onlineexchange.controller;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.ImageObserver;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.extensions.event.ResizeEvent;
import org.primefaces.extensions.event.RotateEvent;
import org.primefaces.model.DefaultStreamedContent;

import com.ews.onlineexchange.dao.SimpleEntityManagerDao;
import com.ews.onlineexchange.model.Image;

@ManagedBean  
@ViewScoped  
public class ImageController implements Serializable {  
  
    private static final long serialVersionUID = 20111020L;  
    private String imageId;
  
  
	public String getImageId() {
		return imageId;
	}

	public void setImageId(String imageId) {
		this.imageId = imageId;
	}
	@Inject
    private SimpleEntityManagerDao dao;
	
	public void rotateImage () throws Exception {
//		ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
		HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String id = request.getParameter("imageId");
		Image image = dao.find(Image.class, id);
			File file = new File("c://my.png");

		    OutputStream output = new FileOutputStream(file);

		    BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

		    bufferedOutput.write(image.getFiledata());
		    java.awt.Image ima = ImageIO.read(file); 
		   
//		    BufferedImage dimg = new BufferedImage(w, h, img.getType());\
		    Graphics g =  ima.getGraphics();
//		    AffineTransform identity = new AffineTransform();
		    Graphics2D g2d = (Graphics2D)g;
//		    AffineTransform trans = new AffineTransform();
//		    trans.setTransform(identity);
//		    trans.rotate( Math.toRadians(45) );
		    g2d.rotate(Math.toRadians(45));
		    
    	
    }

	

	
}  